package com.atguigu.daijia.dispatch.xxl.job;

import com.atguigu.daijia.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.daijia.dispatch.service.NewOrderService;
import com.atguigu.daijia.model.entity.dispatch.XxlJobLog;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 任务Job方法
 * @Author: wangshuo
 * @Date: 2024/8/1 15:01
 */
@Component
public class JobHandler {

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;

    @Autowired
    private NewOrderService newOrderService;

    @XxlJob("newOrderTaskHandler")
    public void newOrderTaskHandler() {
        //向xxl_job_log日志记录表中添加数据
        XxlJobLog xxlJobLog = new XxlJobLog();
        xxlJobLog.setJobId(XxlJobHelper.getJobId());
        long startTime = System.currentTimeMillis();

        try {
            //执行任务 ：搜索附近可以代驾的司机
            //将执行任务的代码放在service中完善
            newOrderService.executeTask(XxlJobHelper.getJobId());

            //成功状态值
            xxlJobLog.setStatus(1);

        } catch (Exception e) {
            //失败状态
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
            e.printStackTrace();
        } finally {
            xxlJobLog.setTimes(System.currentTimeMillis() - startTime);
            xxlJobLogMapper.insert(xxlJobLog);
        }
    }

}
